//
//  MyRankingDetailVC.swift
//  LeGou
//
//  Created by 刘亿前 on 2022/9/16.
//

import UIKit

class MyRankingDetailVC: ZMViewController {
    
    lazy var listTable: UITableView = {
        let table = UITableView(frame: CGRect(), style: .plain)
        table.delegate = self
        table.dataSource = self
        table.separatorStyle = .none
        table.register(UINib(nibName: "MyRankingDetailCell", bundle: nil), forCellReuseIdentifier: "MyRankingDetailCell")
        table.showsVerticalScrollIndicator = false
        table.tableFooterView = UIView()
        table.layer.cornerRadius = 12
        table.layer.masksToBounds = true
        view.addSubview(table)
        table.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.bottom.equalToSuperview()
//            make.height.equalTo(SCREEN_H - 12)
        }
        addRefresh(scrollView: table) { [unowned self] refresh in
            self.page = refresh ?  1 : self.page + 1
            self.requestList()
        }
        table.mj_footer?.isHidden = true
        return table
    }()
    
    var page = 1
    let pageSize = 10
    var dataArr = [RankingDetailModel]()

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "累计额度明细"
        view.backgroundColor = "#F5F5F5".zm.hex()
        listTable.mj_header?.beginRefreshing()
        emptyImg = "nodata"
        emptyTitle = "暂无数据"
        bindAction()
    }

    // MARK: - bindAction
    func bindAction() {
//        listTable.rx.observeWeakly(CGSize.self, "contentSize").subscribe(onNext: { [unowned self] (change) in
//            self.listTable.snp.updateConstraints { make in
//                make.height.equalTo(change!.height == 0 ? 200 : change!.height > SCREEN_W - 12 ? SCREEN_W - 12 : change!.height)
//            }
//        }).disposed(by: disposeBag)
    }
}


extension MyRankingDetailVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 68
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyRankingDetailCell", for: indexPath) as! MyRankingDetailCell
        let info = dataArr[indexPath.row]
        cell.title.text = info.title
        cell.time.text = info.createTime
        cell.number.text = (info.pm == 1 ? "+" : "-") + info.number.zm.toString(keep: 2)
        return cell
    }
}

// MARK: - Request Action
extension MyRankingDetailVC {
    func requestList() {
        let pa = ["limit": pageSize, "page": page] as [String : Any]
        ZMUtil.listRequest(type: RankingDetailModel.self, api: MineAPI.quotaBill(pa)) { [unowned self]
            resp in
            if resp.code == .success, let data = resp.data {
                self.relodaData(data)
            } else {
                self.relodaData()
            }
        }
    }
    
    func relodaData(_ data: [RankingDetailModel] = []) {
        addNodataScrollView(scrollView: listTable)
        listTable.mj_header?.endRefreshing()
        listTable.mj_footer?.endRefreshing()
        if page == 1 {
            dataArr.removeAll()
        }
        dataArr.append(contentsOf: data)
        if data.count < pageSize {
            listTable.mj_footer?.endRefreshingWithNoMoreData()
        }
        listTable.mj_footer?.isHidden = dataArr.count == 0
        listTable.reloadData()
    }
}


struct RankingDetailModel: HandyJSON {
    var title = ""
    var createTime = ""
    // 明细数字
    var number = 0.0
    // 0 = 支出 1 = 获得
    var pm = 0
    // 发放进度百分比（1-100）
    var speed = 0.0
    // 当月任务情况
    var monthTask = false
}
